import { useState } from "react";

type Option = {
  key: string;
  isMultiple: boolean;
};
const useCheck = <T>({ key, isMultiple = true }: Option) => {
  const [checked, setChecked] = useState<T[]>([]);
  const handleCheck = (data: T) => {
    if (isMultiple) {
      if (checked.find((item) => item[key] === data[key])) {
        setChecked(checked.filter((item) => item[key] !== data[key]));
      } else {
        setChecked([...checked, data]);
      }
    } else {
      setChecked([data]);
    }
  };
  const handleClearCheck = () => {
    setChecked([]);
  };

  return { checked, handleCheck, handleClearCheck };
};

export default useCheck;
